/*
 * MorseME.java
 *
 * Created on May 15, 2008, 2:07 AM
 */
package morseme;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import com.nokia.mid.sound.*;
import com.nokia.mid.ui.DeviceControl;
import java.io.*;

/**
 *
 * @author  tt
 * @version
 */
public class MorseME extends MIDlet {

    private static MorseME morseME;
    private SMSServer server;
    public static int DEFAULT_PORT = 6535;
    private static final int NOTE_LOW = 84;
    private static final int NOTE_HIGH = 89;
    private static Sound sound;

    public MorseME() {

        try {

            morseME = this;
            //String df = this.getAppProperty("Default-Port");
            //DEFAULT_PORT = Integer.parseInt(df);
            server = new SMSServer(DEFAULT_PORT,this);

            sound = new Sound(0, 1L);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    protected void startApp() {



        Display display = Display.getDisplay(this);
        display.setCurrent(new MainMenu());
    }

    protected void pauseApp() {

    }

    protected void destroyApp(boolean unconditional) {
    }

    public static MorseME getMIDlet() {

        return morseME;
    }

    public void play(final int note, final int duration) {

        try {
            if (Option.SOUND_SEL == 0) {
                //sound.init(frequency, duration);
                //sound.play(1);
                new Thread(){
                    public void run(){
                        try{
                            Manager.playTone(note,duration,100);
                        }catch(MediaException me){
                            me.printStackTrace();
                        }
                    }
                }.start();
                
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void vibrate(final int dur) {
        new Thread(){
            public void run(){
                if (Option.VIBRATION_SEL == 0) {
                    //DeviceControl.startVibra(freq, dur);
                    Display.getDisplay(MorseME.this).vibrate(dur);
                }
            }
        }.start();
        
    }

    public void play(String morseMsg) {

        if (Option.SOUND_SEL == 1 && Option.VIBRATION_SEL == 1) {
            return;
        }

//Play Start Sound
        play(NOTE_HIGH, 1000);


        try {
            Thread.sleep(1000);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        for (int i = 0; i < morseMsg.length(); i++) {
            int sleep = 0;
            char c = morseMsg.charAt(i);
            switch (c) {

                case '|':
                    sleep = 5000;
                    break;

                case '-':
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex1) {
                    }
                    vibrate(2000);
                    sleep = 2500;
                    play(NOTE_LOW, 2000);
                    break;

                case '.':
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex1) {
                    }
                    vibrate(250);
                    sleep = 250;
                    play(NOTE_LOW, 250);
                    break;

                case ' ':
                    sleep = 3000;
                    break;
            }
            try {
                Thread.sleep(sleep);

            } catch (Exception ex) {
                stop();
            }

        }

    }

    public void stop() {

        try {

            if (sound != null) {
                sound.stop();
            }
            DeviceControl.stopVibra();

        } catch (Exception ex) {
            System.out.println(")))))))))))4");
            ex.printStackTrace();
        }
    }

    public void quitApp() {

        try {
            if (server != null) {
                server.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        destroyApp(true);
        notifyDestroyed();
    }
}

